import { Geometry } from "./Geometry";
import { any, all, has } from "../core/logic";
import { process } from "../process/worker/process";
/**
 * @class
 * @memberof w.geometry
 * @extends w.geometry.Geometry
 */
class RectangleGeometry extends Geometry {
  /**
   * 矩形几何体的构造方法。
   * @constructor
   * @param {*} opts
   * @param {Boolean} opts.back 【选填】对圆形的解析是否放在后台，缺省：true，在后台进行解析。
   * @param {Number} opts.width 【选填】矩形宽度。缺省：1。
   * @param {Number} opts.height 【选填】矩形高度。缺省：1。
   * @param {Number} opts.repeatSeg 【选填】当纹理映射时重复段数计算。缺省：1,不重复。
   * @example
   * let rect=new w.geometry.Rectage({width:2,height:1});
   */
  constructor(opts = {}) {
    super(opts);
    this.type = "RectangleGeometry";
    this.position = null;
    this._width = any(opts.width, 1);
    this._height = any(opts.height, 1);
    this._repeatSeg = any(opts.repeatSeg, 1);
    this.back = any(opts.back, false);
    this.createRectage();
  }

  /**
   * @property {Boolean} back 解析逻辑的选择，true:后台解析；false:前台解析。
   */
  set back(v) {
    this._back = v;
    return this;
  }

  get back() {
    return this._back;
  }

  /**
   * @property {Number} width 矩形宽度。
   */
  set width(v) {
    this._width = v;
    this.createRectage();
    return this;
  }

  get width() {
    return this._width;
  }

  /**
   * @property {Number} height 矩形高度。
   */
  set height(v) {
    this._height = v;
    this.createRectage();
    return this;
  }

  get height() {
    return this._height;
  }
  /**
 * @property {Number} repeatSeg 重复段。
 */
  set repeatSeg(v) {
    if (v != this._repeatSeg) {
      this.texCood0 = new Float32Array([0, 0, 0, v, v, v, v, 0]);
      this._repeatSeg = v;
    }
    return this;
  }

  get repeatSeg() {
    return this._repeatSeg;
  }

  createRectage() {
    if (all(has(this.width), has(this.height))) {
      let info = { back: this.back, width: this.width, height: this.height, repeatSeg: this.repeatSeg, type: "createRectage" }
      process(info, (message) => {
        let { position, indices, uv } = message.data;
        this.indices = indices;
        this.position = position;
        this.texCood0 = uv;
      })
    }
  }


}

export { RectangleGeometry }